<template>
  <div id="MenuListDetails">
    <div class="wrap">
      <div id="content">
        <div class="left">
          <div class="menu-head">
            <div class="menu-title"><h3>{{ menuFatherName }}</h3></div>
            <div class="collect">
              <el-button class="collectbtn" @click="handlecollect">{{ collectText }}</el-button>
            </div>
            <div class="auther">
               <router-link :to="{name: 'CenterRecipes',params: {  userId:userId}}">
                <div class="auther-img"><img :src="headImg" alt=""></div>
              </router-link>
              <div class="auther-info"><a href="#">{{ nickname }}</a></div>
              <a href="javascript:void(0);" class="addicon" @click="handlefocus">
                <!-- <span class="add" >+</span> -->
                {{ focusText }}</a>
            </div>
            <p>{{ menuFatherIntroduce }}</p>
            <div class="menu-info">
              <span>发布时间</span> {{ menuFatherReleaseTime }}
            </div>
          </div>
          <div class="orderList" v-if="menulistdata === '' || menulistdata.length ===0">菜单下没有食谱</div>
          <div v-else class="menu-list">
            <ul class="menus">
              <li class="menupart" v-for="p in menulistdata" :key="p.menuId" @click="jump(p.menuId)">
                <a>
                  <img :src="p.menuSrc" alt="">
                </a>
                <a class="cookname">{{ p.menuName }}</a>
                <a class="nickname">
                  <span>by
                    {{ p.nickname }}
                  </span>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// import cookie from '../../cookien'
// import LoginVue from '../Login.vue'

export default {
  name: 'MenuListDetails',
  data () {
    return {
      collectimg: '',
      collect: false,
      focus: false,
      menuFatherName: '',
      headImg: '',
      menuFatherReleaseTime: '',
      nickname: '',
      menuFatherIntroduce: '',
      menulistdata: [],
      collection: '',
      releaseTime: '',
      cookTime: '',
      doh: '',
      userId: ''
    }
  },
  computed: {
    collectText () {
      return this.collect ? '已收藏' : '收藏'
    },
    focusText () {
      return this.focus ? '已关注' : '关注'
    }
  },
  created () {
    this.menulist().then(data => {
      // 判断收藏
      console.log(this.$store.state.token !== undefined && this.$store.state.token !== '')
      if (this.$store.state.token !== undefined && this.$store.state.token !== '') {
        this.judgecollect()
      }
      // 判断关注
      if (this.$store.state.token !== undefined && this.$store.state.token !== '') {
        this.judgefocus()
      }
    }).catch(err => {
      console.log(err.toString())
    })
  },
  mounted () {
    console.log(this.$route.params.id)
  },
  methods: {
    // 跳转
    jump (h) {
      this.$router.push({
        name: 'DishDetails',
        params: {
          id: h
        }
      })
    },
    // 渲染菜谱列表
    menulist () {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: 'get',
          url: '/Menu/Visitor/findMenuByMenuFatherMenuIds',
          params: {
            MenuFatherId: this.$route.params.id
          }
        }).then(data => {
          console.log(data.data)
          this.menulistdata = data.data.object
          this.menuFatherName = data.data.object1.menuFatherName
          this.menuFatherIntroduce = data.data.object1.menuFatherIntroduce
          this.headImg = data.data.object1.headImg
          this.menuFatherReleaseTime = data.data.object1.menuFatherReleaseTime
          this.nickname = data.data.object1.nickname
          this.userId = data.data.object1.userId
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 判断是否收藏
    judgecollect () {
      this.$axios({
        method: 'post',
        url: '/Menu/User/judgeOfCollection',
        params: {
          menuId: this.$route.params.id,
          menuType: 1,
          userId: this.$store.state.userMsg.userId
        }
      }).then(data => {
        console.log(data)
        if (data.data.code === 501) {
          this.$message(data.data.message)
        } else {
          if (data.data.message === '已经收藏') {
            this.collect = !this.collect
          } else {
            this.collect = false
          }
        }
      })
    },
    // 收藏功能
    handlecollect () {
      if (this.$store.state.token !== undefined && this.$store.state.token !== '' && this.$store.state.token !== null && this.$store.state.token !== 'null') {
        this.$axios({
          method: 'post',
          url: '/Menu/User/collection',
          params: {
            menuId: this.$route.params.id,
            menuType: 1,
            userId: this.$store.state.userMsg.userId
          }
        }).then(data => {
          console.log(data)
          this.collect = !this.collect
        })
      } else {
        this.$message('请先登录')
      }
    },
    // 判断是否关注
    judgefocus () {
      this.$axios({
        method: 'post',
        url: '/Menu/User/judgeOfAttention',
        params: {
          userOneId: this.$store.state.userMsg.userId,
          userTwoId: this.userId
        }
      }).then(data => {
        console.log(data)
        if (data.data.code === 501) {
          this.$message(data.data.message)
        } else {
          if (data.data.message === '已经关注了') {
            this.focus = !this.focus
          } else {
            this.focus = false
          }
        }
      })
    },
    // 关注功能
    handlefocus () {
      if (this.$store.state.token !== undefined && this.$store.state.token !== '' && this.$store.state.token !== null && this.$store.state.token !== 'null') {
        this.$axios({
          method: 'post',
          url: '/Menu/User/attention',
          params: {
            userOneId: this.$store.state.userMsg.userId,
            userTwoId: this.userId
          }
        }).then(data => {
          console.log(data)
          this.focus = !this.focus
        })
      } else {
        this.$message('请先登录')
      }
    }
    // 渲染菜谱列表
    // menulist () {
    //   return new Promise((resolve, reject) => {
    //     this.$axios({
    //       url: '/Menu/Visitor/findMenuByMenuFatherMenuIds',
    //       params: {
    //         MenuFatherId: this.$route.params.id
    //       }
    //     }).then(data => {
    //       this.userId = data.data.object1.userId
    //       this.menulistdata = data.data.object
    //       this.menuFatherName = data.data.object1.menuFatherName
    //       this.menuFatherIntroduce = data.data.object1.menuFatherIntroduce
    //       this.headImg = data.data.object1.headImg
    //       this.menuFatherReleaseTime = data.data.object1.menuFatherReleaseTime
    //       this.nickname = data.data.object1.nickname
    //       this.headImg = data.data.object1.headImg
    //       resolve(data.data.object1.userId)
    //     }).catch(err => {
    //       reject(err)
    //     })
    //   })
    // }
  }
}
</script>
<style lang="less" scoped>
#MenuListDetails {
  margin-top: 60px;
  overflow: auto;
}

.orderList{
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  margin-top: 20px;
  color: #ffb31a;
}

a {
  color: #333;
  text-decoration: none;
}

.wrap {
  #content {
    width: 1000px;
    margin: 0 auto;
    .right {
      width: 280px;
      // background-color: yellow;
      float: left;
      margin-top: 30px;

      h2 {
        font-weight: bold;
        color: #333;
        font-size: 16px;
        margin-left: 15px;
      }

      .heads {
        width: 90%;
        height: 300px;
        // background: olive;
        margin-top: 15px;
        margin-left: 15px;
        margin-bottom: 15px;

        li {
          width: 40px;
          height: 40px;
          margin-right: 8px;
          margin-bottom: 12px;
          border-radius: 100%;
          overflow: hidden;
          float: left;
          // background-color: #FFB31A;
          a {
            color: #333;
            text-decoration: none;

            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
    }

  }

  .left {
    width: 690px;
    float: left;
    margin-top: 30px;
    .menu-head {
      position: relative;

      .menu-title {
        padding-top: 5px;
        width: 565px;

        h3 {
          font-weight: bold;
          font-size: 22px;
          line-height: 26px;
        }
      }

      .collect {
        position: absolute;
        top: 0;
        right: 0;

        .collectbtn {
          background-color: #FFB31A;
          border: none;
          color: #fff;
          width: 85px;

          i {
            margin-left: -8px;
            margin-right: 4px;
          }
        }
      }

      .auther {
        margin: 25px 0px;
        display: flex;

        .addicon {
          text-decoration: none;
          display: inline-block;
          width: 80px;
          text-align: center;
          height: 30px;
          line-height: 28px;
          font-size: 12px;
          border-radius: 4px;
          color: #333;
          border: 1px solid #333;
        }

        .auther-img {
          width: 30px;
          height: 30px;
          margin-right: 6px;
          border-radius: 50%;
          img {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            margin-right: 6px;
          }
        }

        .auther-info {
          float: left;
          margin-right: 20px;
          margin-left: 12px;

          a {
            display: inline-block;
            font-size: 13px;
            line-height: 30px;
            vertical-align: middle;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            color: #333;
            text-decoration: none;
          }
        }

        p {
          font-size: 15px;
          line-height: 27px;
          text-align: left;
        }
      }
    }
  }
}

.menu-info {
  padding: 15px 0 40px;
  border-bottom: 1px solid #d8d8d8;
  color: #999;
  font-size: 13px;
}

.menu-list {
  padding-top: 30px;
  min-height: 600px;

  .menus {
    list-style: none;
    display: flex;
    //justify-content: space-between;
    flex-wrap: wrap;

    .menupart {
      width: 220px;
      height: 275px;
      margin-right: 10px;
      margin-bottom: 27px;
      cursor: pointer;
      // background-color: brown;
      img {
        width: 220px;
        height: 220px;
        border-radius: 8px;
      }

      .cookname {
        font-size: 15px;
        line-height: 35px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }

      .nickname {
        display: block;
        font-size: 12px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }
}
</style>
